import base64
import json
import time
import uuid

import requests
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding

# 提供的私钥（PEM 格式）
private_key_pem = """-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCdgJNfUFPDNJsL
HObB1JMu7E1+nuwkFHmXnBU2AOM2dweE+tpmViZo90w+YQIuIS8MoVz60AGHbLE8
BYcdxQEKmPsqq0Lq/1ltIdp1YcO9W60qSxwpZS+7o73ljRrrtOXcE1UUpH5l07Fh
ziCIRDI/4ODCA8AJ1kV6IyfPNM2Fes3BEqhMOgw4Z5i4pZHnb4Nm+4kEXmyM+UgQ
cShcXZA/dx5MXKA2Bbb0I0G6HS3D4nMhnm6IgYWEyT8ngenMOyy+ysBuHWt2j9Cp
AGLWRyqHigFcKTlP5BSIkU+8sqssab1jvDg2F8MXWuupwF43OVARgHofiwQBAHPo
PfTfPlMvAgMBAAECggEBAJKQpZNasrfCak0LFgllgZl2uB6OUPy6OPRGgM6CQO3c
EhlDPp1gqdmf10ltCJRYuOmt91JG4kVddgh+tF+VhgSQm5n3SQxZlqQhjqMQ2Q+L
Ejd7Mberu6GHHB1TE6wn6IbFTrUo5Z5oQnbbVBa6L3CWGVEyIDCHPpwLvu3pGx+L
083dNQUiF8WcSGybl1h4ZapAGdndPYJReKYccNBYu5IzTEjtG3VpMHl56hD8fPV8
SStYv4sEffyCbze5/KvG3WlG+8n1WzBRMAN1U8Qk3JlMM/g5Y2tL1elI2pQRmjH8
EVxNUzB9Ob/qk2N6pF4KwhDWjILkHdoXilHMgP5x0gECgYEAy9O9ShtRNwXdFzHe
v+buyjvWWvwTVRUBehe8BWO1QaZ4c/INw1Ks4pgoKvXyU1DRx5OloIx6BWDbs00O
1W1cDue2I/Ymvx5Q/XJmZK4eR2U3a2dmKLKVhCXhJ3y02R/OZ2xQHV3NZXqz88kf
rEmEKYTW9q2gVsZa82XpQhKnBYECgYEAxdFJ55AkU7VfzpV1x68NUetomB3OWxyq
Cugn3STLNx7Jw6FaK3dwRz0eKIbwCRxtlluZmxWX0jWSvj3cyLRBIKTD8atUfJW2
+ESKZb/i961HhhQjXqNfGQpmMdEazNqv0sDzQ5jHHIjc63oty/FjckcC+AaDGZIJ
VGCet5J5kK8CgYBm2R/Bfgk792R5KLvaHz/MoebmoB1tKB1HqyQ/n/E9AC/1aWUS
cuwzpk1WaCXvbm98Af9oBJopjpctYSuj+/ugtcDNYo5oj3aUfJ44HTfAFM2jD1iY
HoydUrPKxf1HNepje17tgoB6vTCCSbEGsU3T2WjSrgei4ZHREVJi+aB3gQKBgEy8
rm2sxdrPHjZWVlU6+/DOYEm6LkW77d7DRkuMLWTZha1lF0SLVbvc4qkYB1+RbpWI
PSMjEj0SWTWBa/dTrXwLTpOeQez+avcOJ53m/RXVW0yQ3VOmDor5NMGYe0wCfXhF
L1kGmB7inMigIcnefxRipa0vYYX217WqsYdGw++zAoGBALKswyV5j1GjVjN+fS1t
N9R0x+S7cKBqW6Bwj6aAdo4+spmRn9WK4h9Zk2k7BMUiqJKTce6RdW0Ep+aTErRs
LL0sBHArhQdaQvq0yS57BJUZm3ASrOpp3wkQdDejS3YEKiIQSG2kNFRanh8RbtbA
ac7pfLikyQm795/qF0H9YHgF
-----END PRIVATE KEY-----"""

headers = {
    "Origin": "https://www.baiyunairport.com",
    "Referer": "https://www.baiyunairport.com/flight/dep",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
    "content-type": "application/json;charset=UTF-8",
    "Signature": "DkD+AjzfRblCt/zcVxGE/uNoQHBoSPgOvch2M5Pzlg4GNl8L4pVPzu4zmoZovJ1yJmmaz1OptBIlGqvMisT9CvNg2eyu9ryGLEaY2b3mDV+2+QkrxEmKpVBBtgyfa81IoBx8/TrTFFA1iJ6B+pe5QAxCjQu87oPp2B3O+yOMTDP4Fu98THZSiP/aOvEcLr7XSmPjb+PmhwQ59bwQGpKf0zp0IOGd2TRYyXB6Gi9DhvDB+fgkiA1SYQ7DMAafCCeC9e9efKGZ3XPhSKhweacAQC/0imq3KdNkOz7YDt9iNOnxShfrvL4zc8QGb3UwPOOipKvheP8Mux+oYbGGUWkOtQ==",
    "timestamp": f"{int(time.time() * 1000)}"
}
data_1 = {
    "type": "1",
    "terminal": "",
    "day": 0,
    "depOrArr": "1",
    "pageNum": 1,
    "pageSize": 15
}
aa = json.dumps(data_1)
# 待签名的数据
data_3 = f'Timestamp&"{headers["timestamp"]}"&/byairport-flight/flight/searchFlight&{str(uuid.uuid4()).replace("-","")}&'
data = data_3 + aa


# 加载私钥
private_key = serialization.load_pem_private_key(private_key_pem.encode(), password=None, backend=default_backend())

# 计算数据的 SHA-256 哈希值
digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
digest.update(data.encode())
hash_value = digest.finalize()

# 使用私钥进行 SHA256withRSA 签名
signature = private_key.sign(
    hash_value,
    padding.PKCS1v15(),  # 使用PKCS#1 v1.5 填充方案
    hashes.SHA256()  # 使用SHA-256 哈希算法
)

# 将签名结果进行 Base64 编码
base64_signature = base64.b64encode(signature).decode()

headers["Signature"] = base64_signature


url = "https://www.baiyunairport.com/byairport-flight/flight/list"
data_2 = json.dumps(data_1, separators=(',', ':'))


response = requests.post(url, headers=headers, data=data_2)

print(response.text)
print(response)
